<?php
namespace app\wxapp\controller;

use app\model\Record; 
use app\model\Theme; 
use app\model\Fans; 
use app\model\Share; 
use app\model\Scan; 

class Friendhelp extends Base
{
    public function Friendhelp()
    {
global $_P, $_G;
$fans = $this->checkuser();
$system=$_G['system'];
// var_dump($system);
$themeid = intval($_P['themeid']);
$theme = Theme::getThemeByIdxcx($themeid);
if(empty($theme['id'])){
    $this->result(1, '活动入口不正确');
    exit;
}

$op = !empty($_P['op']) ? $_P['op'] : 'display';
$config = Theme::getConfig($themeid);
$fuid = $_G['member']['uid'];
$uid = $_P['fromuid'];
if (empty($config['friend_name'])){
    $config['friend_name']="点亮";
}

if(empty($fuid)){
    $this->result(1, '用户信息错误');
    exit;
}

if($uid == $fuid || empty($uid)){
    $this->result(1, '不可给自己'.$config['friend_name']);
    exit;
}

if($config['friendorder']==0){
    $friendorder = Record::ispay($config,$uid,$themeid);
    if(empty($friendorder)){
        $this->result(1, '该用户还没有报名');
        exit;
    }
}

if($config['isicon'] !=3){
    $this->result(1, '功能未开启');
    exit;
}

$params = array();

$params[':uid'] = $fuid;
$params[':fromuid'] = $uid;
$params[':themeid'] = $themeid;
$friend = db_find("SELECT * FROM ".tablename(t_friend)."
		WHERE uid = :uid and themeid = :themeid and fromuid = :fromuid",$params);

if(empty($friend)){
    $data = array(
        'themeid'   => $themeid,
        'uniacid'   => $_G['mpid'],
        'uid'       => $_G['member']['uid'],
        'openid'    => $_G['openid'],
        'nickname'  => $fans['nickname'],
        'avatar'    => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
        'status'    => 0,
        'addtime'   => TIMESTAMP,
        'fromuid'  => $uid,
        'fopenid'  => $fopenid,
        'workerid' => $workerid,
    );

    if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
        $data['workerid_old']  = $workerid;
    }

    if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
        $data['workerid_old']  = $workerid;
    }
    // if($config['iconlimit'] > 0){
    //     $todaynum = Scan::getFpenidTodayNum($themeid,$fopenid);
    //     if($todaynum < $config['iconlimit']){
    //         $data['status'] = 1;
    //     }
    // }else{
    $data['status'] = 1;
    // }
    $insertid = db_insert(t_friend, $data);

    $pm = array(":themeid"=>$themeid,":uid"=>$uid);

    db_query("UPDATE ".tablename(t_fans)." SET friendnum = friendnum +1 WHERE themeid =:themeid and uid = :uid",$pm);
    $helpfans = Fans::getById($uid,$themeid);


    //更新点亮数
    $lightsum = 0;
    if($config['iconnum2'] <= 0){
        $lightsum = Share::getNumByThemeIdAndOpenidAndStatus($themeid,$_G['openid'],1);
    }else{
        if ($config['isicon']==2) {
            $lightsum = Scan::getNumByThemeIdAndOpenid($themeid,$_G['openid'],1);;
        }else{
            $lightsum = Scan::getNumByThemeIdAndOpenidAndStatus($themeid,$_G['openid'],1);;
        }
        $lightsum = intval($lightsum/$config['iconnum2']);
    }

    if ($config['isicon']==3) {
        $help_uid = $_P['fromuid'];
        if (!empty($help_uid)){
            $friendlistuid = $help_uid;
        }elseif (!empty($themyrecord) || $config['friendorder']==1){
            $friendlistuid = $_G['member']['uid'];
        }
        if (!empty($friendlistuid)){
            $friendfans = Fans::getById($friendlistuid,$themeid);
            $lightsum = $friendfans['friendnum'];
        }else{
            $lightsum = 0;
        }
        if (!empty($friendlistuid)){
            $friendfans = Fans::getById($friendlistuid,$themeid);
            $params = array();
            $params[':uniacid'] = $_G['mpid'];
            $params[':uid'] = $_G['member']['uid'];
            $params[':fromuid'] = $help_uid;
            $params[':themeid'] = $themeid;
            $friend = db_find("SELECT * FROM ".tablename(t_friend)."
                    WHERE uniacid= :uniacid and uid = :uid and themeid = :themeid and fromuid = :fromuid",$params);

            if(empty($friend)){
                $isfriend = 0;
            }else{
                $isfriend = 1;
            }
        }

    }

    if ($config['iconlogo']!=1) {
        if($lightsum >= $config['iconnum']){
            $lightsum = $config['iconnum'];
        }

        $lightlist = array();
        for($i=1;$i<=$lightsum;$i++){
            $lightlist[$i]=$i;
        }
        $darksum = $config['iconnum'] ;
        $darklist = array();

        for($i=$lightsum+1;$i<=$darksum;$i++){
            $darklist[$i]=$i;
        }
    }else{
        $shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
            " WHERE  uniacid = '{$_G['mpid']}' and state = 1 and darkicon<>'' order by displayorder" );
        $iconnum = count($shoplist);

        if($lightsum >= $iconnum){
            $lightsum = $iconnum;
        }

        $lightlist = array();
        $darklist = array();
        foreach ($shoplist as $index => $item) {
            $i = $index+1;
            if ($i<=$lightsum) {
                $lightlist[$i] = str_replace("/99.jpg","/logo.jpg",$item['logo']);
            } else {
                $darklist[$i] = str_replace("/99.jpg","/logo.jpg",$item['darkicon']);
            }
        }
    }
    if ($config['iconlogo']!=1){
        foreach ($lightlist as $index => $item) {
            $lightlist[$index]=tomedia($config['lighticon']);
        }
        foreach ($darklist as $index => $item) {
            $darklist[$index]=tomedia($config['darkicon']);
        }
    }else{
        foreach ($lightlist as $index => $item) {
            $lightlist[$index]=tomedia($item);
        }
        foreach ($darklist as $index => $item) {
            $darklist[$index]=tomedia($item);
        }
    }
    $result = array(
        'lightlist' => $lightlist,
        'darklist' => $darklist,
        'message' => '帮【'.$helpfans['nickname'].'】'. $config['friend_name'].'成功',
    );

    $this->result(0, '成功', $result);
    exit;
}else{
    $this->result(1, $config['friend_name'].'失败,您只可帮好友'.$config['friend_name'].'1次');
    exit;
}
}
}